package de.bazinga.rotw.sound;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Hashtable;

import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;

import de.bazinga.rotw.game.Game;

public class Sound {

	private static Sound sounds = null;
	
	private Hashtable<String,AudioTrack> audioTracks = new Hashtable<String,AudioTrack>();
	private AudioSystem audio;
	
	public Sound() {
		audio = AudioSystem.getSystem();
		audio.getEar().trackOrientation(Game.getGame().getCamera());
		audio.getEar().trackPosition(Game.getGame().getCamera());
	}
	
	public static Sound getInstance() {
		if(sounds == null) {
			sounds = new Sound();
		}
		return sounds;
	}
	
	public void addSound(String fileName) {
		try {
			AudioTrack audioTrack = audio.createAudioTrack(new File(fileName).toURI().toURL(), false);
			audioTrack.setMaxAudibleDistance(1000);
			audioTrack.setVolume(1.0f);
			
			audioTracks.put(fileName, audioTrack);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			System.out.println("Couldn't load Sound: "+fileName);
		}		
	}
	
	public AudioTrack getAudioTrack(String fileName) {
		if(!audioTracks.contains(fileName)) {
			addSound(fileName);
		}
		return audioTracks.get(fileName);
	}
}